import 'package:environmental_les_app/common/routes/route_names.dart';
import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/bottom_select_drawer.dart';
import 'package:environmental_les_app/common/widgets/button_public_rect.dart';
import 'package:environmental_les_app/common/widgets/common_widget.dart';
import 'package:environmental_les_app/common/widgets/required_field_text.dart';
import 'package:environmental_les_app/common/widgets/text_area.dart';
import 'package:environmental_les_app/page/task_list/create_task/widgets/enterprise_selection.dart';
import 'package:environmental_les_app/page/task_list/create_task/widgets/personnel_selection.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'index.dart';

class ReportProblemPage extends GetView<ReportProblemController> {
  const ReportProblemPage({super.key});

  // 主视图
  List<Widget> _buildView() {
    return [
      baseCardContainer(
          padding:
              const EdgeInsets.only(top: 0, left: 12, right: 12, bottom: 16),
          content: [
            operateRowItem(
                title: "完成时间",
                content: "2023-03-15 10:00",
                isRequired: true,
                onTap: () {},
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "派遣方式",
                hintText: "请选择派遣方式",
                content: controller.selectedDispatchMethod['name'] ?? '',
                isRequired: true,
                onTap: () {
                  var contentList = controller.dispatchMethodList
                      .map<String>((e) => e['name'])
                      .toList();

                  Get.bottomSheet(BottomSelectDrawer(
                    title: "派遣方式",
                    contentList: contentList,
                    completeSelect: (data, index) {
                      controller.modelSelect(index);
                    },
                  ));
                },
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "派遣对象",
                hintText: "请选择派遣对象",
                content: controller.selectedDispatchMethod['value'] == 3
                    ? (controller.selectedEnterprise['name'] ?? '')
                    : (controller.selectedPersonnel['name'] ?? ''),
                isRequired: true,
                onTap: () {
                  if (controller.selectedDispatchMethod['value'] == 3) {
                    Get.to(EnterpriseSelectionWidget(
                      onSelectionChanged: (selected) {
                        controller.selectedEnterprise = selected;
                        controller.update(["report_problem"]);
                      },
                    ));
                    return;
                  }
                  Get.to(
                      PersonnelSelectionWidget(onSelectionChanged: (selected) {
                    controller.selectedPersonnel = selected;
                    controller.update(["report_problem"]);
                  }));
                },
                showArrow: true,
                showLine: true),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                vGap10,
                const RequiredFieldText(
                  isRequired: false,
                  text: "说明",
                  titleColor: Colours.text969,
                  titleFontSize: 15,
                  titleFontWeight: FontWeight.w400,
                ),
                vGap16,
                TextArea(
                  controller: TextEditingController(),
                  hintText: "请输入内容",
                  minLines: 2,
                  maxLines: 5,
                  maxLength: 100,
                ),
              ],
            ),
          ])
    ];
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ReportProblemController>(
      init: ReportProblemController(),
      id: "report_problem",
      builder: (_) {
        return MyScaffold(
          title: "巡查登记",
          bottomButton: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
            child: Row(
              children: [
                Expanded(
                    child: ButtonPublicRect(
                        text: "提交",
                        onPressed: () {
                          Get.toNamed(AppRoutes.reportProblemPage);
                        })),
                hGap10,
                Expanded(
                    child: ButtonPublicRect(
                        text: "取消",
                        type: ButtonPublicType.outline,
                        onPressed: () {
                          Get.toNamed(AppRoutes.patrolFillingPage);
                        })),
              ],
            ),
          ),
          children: [
            ..._buildView(),
            vGap20,
          ],
        );
      },
    );
  }
}
